package com.example.demos.web.controller;

import com.example.demos.dto.OrderDetailsListDTO;
import com.example.demos.dto.OrdersByGoodsDTO;
import com.example.demos.dto.OrdersDTO;
import com.example.demos.entity.Orders;
import com.example.demos.entity.ReturnOrders;
import com.example.demos.entity.Supplier;
import com.example.demos.service.OrdersService;
import com.example.demos.util.PageBean;
import com.example.demos.util.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.math.BigInteger;
import java.util.List;

@RestController
@Slf4j
@RequestMapping("/v2")
public class OrdersController {
    @Autowired
    private OrdersService ordersService;

    @GetMapping("/orders/listByPage")
    public Result<PageBean<List<OrdersDTO>>> listByPage(String state, Integer page, Integer rows) {
        return Result.ok(ordersService.select(state, page, rows));
    }

    @GetMapping("/orders/showsupplier")
    public Result<PageBean<List<Supplier>>> showSupplier(String type) {
        ;
        return Result.ok(ordersService.showSupplier(type));
    }

    @GetMapping("/orders/listByPage1")
    public Result<PageBean<List<OrdersByGoodsDTO>>> listByPage1() {
        return Result.ok(ordersService.select1());
    }

    @PostMapping("/orders/add")
    public Result<Void> add(@RequestBody OrderDetailsListDTO orderDetailsListDTO, HttpSession session) {
        return ordersService.addOrders(orderDetailsListDTO, session) == -1
                ? Result.bizErr("Bad Request") : Result.ok("添加成功");
//        ordersService.add(orderDetailsListDTO.getOrderDetails());

    }

    @PostMapping("/orders/doCheck")
    public Result<Void> doCheck(Integer id, HttpSession session) {
        ordersService.doCheck(id, session);
        return Result.ok("已审核");
    }

    @PostMapping("/orders/doStart")
    public Result<Void> doStart(Integer id, HttpSession session) {
        ordersService.doStart(id, session);
        return Result.ok("已确认");
    }

    @PostMapping("/orderdetail/doInStore")
    public Result<Void> doInStore(@RequestParam("id") BigInteger ordersDetailUuid, @RequestParam("storeuuid") BigInteger storeUuid, HttpSession session) {
        ordersService.doInStore(ordersDetailUuid, storeUuid, session);
        return Result.ok("入库成功");
    }

}
